package com.rainbow.support.action.log.handler;

import com.rainbow.common.core.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.lang.NonNull;

import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * 默认的日志处理
 * @author 付为地
 */
@Slf4j
public class DefaultLogHandler extends AbstractLogHandler {

    /**
     * 执行日志打印
     * @param logData 日志参数
     * @return 日志数据
     */
    @Override
    public SysActionLog handle(@NonNull SysActionLog logData) {
        String type = ObjectUtils.isNotEmpty(logData.getType()) && Objects.equals(Constant.GLOBAL_LOG_ABNORMAL_TYPE, logData.getType()) ? "异常" : "正常";
        String requestParams = Optional.of(logData).map(SysActionLog::getRequestParams).orElse("");
        String responseParams = Optional.of(logData).map(SysActionLog::getResponseParams).orElse("");
        String error = Optional.of(logData).map(SysActionLog::getException).orElse("");
        Boolean showStack = Optional.of(logData).map(SysActionLog::getShowStack).orElse(true);
        Boolean showResult = Optional.of(logData).map(SysActionLog::getShowResult).orElse(true);
        if (Objects.equals(Constant.GLOBAL_LOG_ABNORMAL_TYPE, logData.getType()) && showStack) {
            log.error("\n--------------------------------全局日志开始-----------------------------------------------------------------------------------------------------------------------------------------------\n\t" +
                            "├──>开始==>{} ├──>结束==>{} ├──>耗时==>{}(毫秒) \n\t" +
                            "├──>用户==>{} ├──>链路==>{} ├──>令牌==>{} \n\t" +
                            "├──>项目==>{} ├──>版本==>{} ├──>环境==>{} ├──>类型==>{}\n\t" +
                            "├──>模块==>{} ├──>业务==>{} ├──>方式==>{}\n\t" +
                            "├──>类名==>{} ├──>方法==>{} ├──>地址==>{} \n\t" +
                            "├──>参数==>{} \n\t" +
                            "├──>异常==>{} \n\t" +
                            "----------------------------全局日志结束------------------------------------------------------------------------------------------------------------------------------------------------",
                    DateFormatUtils.format(Date.from(logData.getActionStartTime()), "yyyy-MM-dd HH:mm:ss S"), DateFormatUtils.format(Date.from(logData.getActionEndTime()), "yyyy-MM-dd HH:mm:ss S"), logData.getActionTime(),
                    logData.getUserName(), logData.getTrace(), logData.getToken(),
                    logData.getProject(), logData.getVersion(), logData.getProfile(), type,
                    logData.getMoudle(), logData.getActionType(), logData.getRequestMethod(),
                    logData.getClassName(), logData.getMethodName(), logData.getRequestUri(),
                    requestParams, error);
        } else if (Objects.equals(Constant.GLOBAL_LOG_ABNORMAL_TYPE, logData.getType()) && !showStack) {
            log.error("\n--------------------------------全局日志开始-----------------------------------------------------------------------------------------------------------------------------------------------\n\t" +
                            "├──>开始==>{} ├──>结束==>{} ├──>耗时==>{}(毫秒) \n\t" +
                            "├──>用户==>{} ├──>链路==>{} ├──>令牌==>{} \n\t" +
                            "├──>项目==>{} ├──>版本==>{} ├──>环境==>{} ├──>类型==>{}\n\t" +
                            "├──>模块==>{} ├──>业务==>{} ├──>方式==>{}\n\t" +
                            "├──>类名==>{} ├──>方法==>{} ├──>地址==>{} \n\t" +
                            "├──>参数==>{} \n\t" +
                            "----------------------------全局日志结束------------------------------------------------------------------------------------------------------------------------------------------------",
                    DateFormatUtils.format(Date.from(logData.getActionStartTime()), "yyyy-MM-dd HH:mm:ss S"), DateFormatUtils.format(Date.from(logData.getActionEndTime()), "yyyy-MM-dd HH:mm:ss S"), logData.getActionTime(),
                    logData.getUserName(), logData.getTrace(), logData.getToken(),
                    logData.getProject(), logData.getVersion(), logData.getProfile(), type,
                    logData.getMoudle(), logData.getActionType(), logData.getRequestMethod(),
                    logData.getClassName(), logData.getMethodName(), logData.getRequestUri(),
                    requestParams);
        } else if(Objects.equals(Constant.GLOBAL_LOG_NORMAL_TYPE, logData.getType()) && !showResult){
            log.info("\n--------------------------------全局日志开始-----------------------------------------------------------------------------------------------------------------------------------------------\n\t" +
                            "├──>开始==>{} ├──>结束==>{} ├──>耗时==>{}(毫秒) \n\t" +
                            "├──>用户==>{} ├──>链路==>{} ├──>令牌==>{} \n\t" +
                            "├──>项目==>{} ├──>版本==>{} ├──>环境==>{} ├──>类型==>{}\n\t" +
                            "├──>模块==>{} ├──>业务==>{} ├──>方式==>{}\n\t" +
                            "├──>类名==>{} ├──>方法==>{} ├──>地址==>{} \n\t" +
                            "├──>参数==>{} \n\t" +
                            "----------------------------全局日志结束------------------------------------------------------------------------------------------------------------------------------------------------",
                    DateFormatUtils.format(Date.from(logData.getActionStartTime()), "yyyy-MM-dd HH:mm:ss S"), DateFormatUtils.format(Date.from(logData.getActionEndTime()), "yyyy-MM-dd HH:mm:ss S"), logData.getActionTime(),
                    logData.getUserName(), logData.getTrace(), logData.getToken(),
                    logData.getProject(), logData.getVersion(), logData.getProfile(), type,
                    logData.getMoudle(), logData.getActionType(), logData.getRequestMethod(),
                    logData.getClassName(), logData.getMethodName(), logData.getRequestUri(),
                    requestParams);
        }else{
            log.info("\n--------------------------------全局日志开始-----------------------------------------------------------------------------------------------------------------------------------------------\n\t" +
                            "├──>开始==>{} ├──>结束==>{} ├──>耗时==>{}(毫秒) \n\t" +
                            "├──>用户==>{} ├──>链路==>{} ├──>令牌==>{} \n\t" +
                            "├──>项目==>{} ├──>版本==>{} ├──>环境==>{} ├──>类型==>{}\n\t" +
                            "├──>模块==>{} ├──>业务==>{} ├──>方式==>{}\n\t" +
                            "├──>类名==>{} ├──>方法==>{} ├──>地址==>{} \n\t" +
                            "├──>参数==>{} \n\t" +
                            "├──>返回==>{} \n\t" +
                            "----------------------------全局日志结束------------------------------------------------------------------------------------------------------------------------------------------------",
                    DateFormatUtils.format(Date.from(logData.getActionStartTime()), "yyyy-MM-dd HH:mm:ss S"), DateFormatUtils.format(Date.from(logData.getActionEndTime()), "yyyy-MM-dd HH:mm:ss S"), logData.getActionTime(),
                    logData.getUserName(), logData.getTrace(), logData.getToken(),
                    logData.getProject(), logData.getVersion(), logData.getProfile(), type,
                    logData.getMoudle(), logData.getActionType(), logData.getRequestMethod(),
                    logData.getClassName(), logData.getMethodName(), logData.getRequestUri(),
                    requestParams, responseParams);
        }
        return logData;
    }

}
